var subid = '';
$(function  () {
   
     //获取角色
     $.post(
        ipurl+'/subject/get',
        function(result) {
            if(result.code == 200){
                var menulist = result.data.results;
                var menuhtml = '';
                for(var i=0;i<menulist.length;i++){
                    menuhtml+='<button class="layui-btn" onclick="getclass('+menulist[i].ID+','+"'"+menulist[i].name+"'"+')">'+menulist[i].name+'</button>';
                }
                $("#subject").html(menuhtml);
            }
        }
    );
})



//获取班级
function getclass(id,name){

    $('#tnow').css('display','');
    $('#tnow').html("当前选择学科为:"+name);
    subid=id;
    //获取班级
         //获取班级
         $.post(
            ipurl+'/bclass/get',
            function(result) {
                if(result.code == 200){
                    var classlist = result.data.results;
                    var classhtml = '';
                    for(var i=0;i<classlist.length;i++){
                        classhtml+='<button class="layui-btn" onclick="gettask('+classlist[i].ID+','+"'"+classlist[i].name+"'"+')">'+classlist[i].name+'</button>';
                    }
                    $("#bclass").html(classhtml);
                    $('#bclass').css('display','');
                    //$('#tnow').html("当前选择学科为:"+name);
                }
            }
        );
}






function gettask(id,name){

    $('#bnow').css('display','');
    $('#bnow').html("当前选择班级为:"+name);


    //用户ID
    var uid = $.cookie("lid");

    //学科ID
    var cid = id;
    //根据班级和科目查询作业
    $.ajax({
        url: ipurl+'/task/queryTCData',
        data: {
            CID:cid,
            SID:subid
        },
        type: "POST",
        //dataType: "json",
        success:function(resultper){
                console.log(resultper);
                if(!resultper.success){
                    $("#cnow").html("此班级还未有人交作业");
                    $('#cnow').css('display','');
                    $('#tasktable').css('display','none');
                }else{
                    $('#cnow').css('display','none');
                    var userlist = resultper.data.results;
                    var userhtml = '';
                    for(var i=0;i<userlist.length;i++){
                        userhtml+='<tr>';
                        userhtml+='<td>';
                        userhtml+=userlist[i].USERNAME;
                        userhtml+='</td>';
                        userhtml+='<td>';
                        userhtml+=userlist[i].title;
                        userhtml+='</td>';
                        userhtml+='<td>';
                        userhtml+=userlist[i].content;
                        userhtml+='</td>';
                        userhtml+='<td>';
                        userhtml+=userlist[i].achievement;
                        userhtml+='</td>';
                        userhtml+='<td class="td-manage">';
                        userhtml+='<a title="打分" href="javascript:;" onclick="task_edit('+"'"+'作业打分'+"'"+','+"'"+'scotask-edit.html'+"'"+','+"'"+userlist[i].ID+"'"+','+'500'+','+'510'+')"  class="ml-5" style="text-decoration:none">';
                        userhtml+='<i class="layui-icon">&#xe642;</i>';
                        userhtml+='</a>';
                        userhtml+='<a title="删除" href="javascript:;" onclick="task_del(this,'+userlist[i].ID+')"  style="text-decoration:none">';
                        userhtml+='<i class="layui-icon">&#xe640;</i>';
                        userhtml+='</a>';
                        userhtml+='</td>';
                        userhtml+='</tr>';
                    }
                    $("#taskbody").html(userhtml);
                    $('#tasktable').css('display','');
                }
               
        },
        complete: function(data) {
        }
    
    });

    //$('#tianjia').css('display','');

    
}
    
function task_add(title,url,w,h){
    //$.cookie("prid", roleid, { expires: 7 });
    $.cookie("tsid", subid, { expires: 7 });
    x_admin_show(title,url,w,h);
}

// 打分
function task_edit (title,url,id,w,h) {
    $.cookie("tasid", id, { expires: 1 });
    x_admin_show(title,url,w,h); 
}

/*权限-删除*/
function task_del(obj,id){
    layer.confirm('确认要删除吗？',function(index){
        //发异步删除数据
        $(obj).parents("tr").remove();
        layer.msg('已删除!',{icon:1,time:1000});
        $.post(
            ipurl+'/task/del',
            {
              ID: id
            },
            function(result) {
              console.log(result);
                // layer.open({
                //     content: '添加成功',
                //     end: function(index, layero){
                     
                //     }
                // });
            }
      );
    });
}

